﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Xml.Linq;
using TextEditor.TextBoxInner;
using UniversalEditor.Base;
using UniversalEditor.Base.FileSystem;
using UniversalEditor.Base.MenuBarItems;
using UniversalEditor.Base.StatusBarItems;

namespace TextEditor
{
	public abstract class TextEditorBase : EditorBase
	{
		private readonly StatusTextItem _statusReady = new StatusTextItem { HorizontalAlignment = StatusItemPlacement.Left, Text = "Ready" };
		private readonly StatusTextItem _statusLinesCount = new StatusTextItem { HorizontalAlignment = StatusItemPlacement.Right, TextAlignment = TextAlignment.Right, Text = "Lines: 0" };
		
		protected readonly Lazy<UfeTextBox2> _textBox;
		private XElement _xStatusElement;

		protected TextEditorBase(IEditorOwner owner, FileWrapper file)
			: base(owner, file)
		{
			_textBox = new Lazy<UfeTextBox2>(InitializeTextBox);
		}

		private UfeTextBox2 InitializeTextBox()
		{
			UfeTextBox2 textBox = CreateTextBox();
			
//			textBox.LoadState(_xStatusElement);

//			textBox.LinesCountChanged += UpdateLinesCountState;
//			UpdateLinesCountState(textBox.TextBox.Document.LineCount);

			return textBox;
		}

		private void UpdateLinesCountState(int newLinesCount)
		{
			_statusLinesCount.Text = string.Format("Lines: {0}", newLinesCount);
		}

		protected abstract UfeTextBox2 CreateTextBox();

		public override FrameworkElement EditorControl
		{
			get
			{
				ScrollViewer container = new ScrollViewer { Content = _textBox.Value, CanContentScroll = true };
				_textBox.Value.ScrollOwner = container;

				return container;
			}
		}

		public override void Refresh()
		{
			_textBox.Value.InvalidateVisual();
		}

		public override void Dispose()
		{
//			if (_textBox.IsValueCreated)
//				_textBox.Value.LinesCountChanged += UpdateLinesCountState;

			base.Dispose();
		}

		public override void Reload()
		{
			if (_textBox.IsValueCreated)
			{
//				_textBox.Value.Document.Reload();
//				_textBox.Value.CallOnRender();
			}
		}

		public override bool HasChanges
		{
			get { return _file.HasChanges; }
		}

		public override void Focus()
		{
			_textBox.Value.UpdateLayout();
			_textBox.Value.Focus();
		}

//		public override void SaveStatus(XElement file)
//		{
//			if (_textBox.IsValueCreated)
//				_textBox.Value.SaveStatus(file);
//			else
//			{
//				if (_xStatusElement != null)
//				{
//					foreach (XElement item in _xStatusElement.Elements())
//					{
//						if (file.Element(item.Name) == null)
//							file.Add(new XElement(item));
//					}
//				}
//			}
//		}

		public override void LoadStatus(XElement file)
		{
			_xStatusElement = file;
		}

//		public override void SaveFile()
//		{
//			if (_textBox.IsValueCreated)
//				_textBox.Value.Document.Save(_file.FilePath);
//		}
//
//		public override void SaveFileAs(string filePath)
//		{
//			_textBox.Value.Document.Save(filePath);
//		}
//
//		public override void SaveCopyAs(string filePath)
//		{
//			_textBox.Value.Document.SaveCopy(filePath);
//		}
		
//		public override MenuListItem Menu1
//		{
//			get { return _textBox.Value.GenerateBasicMenu(); }
//		}
//
//		public override MenuListItem Menu2
//		{
//			get { return _textBox.Value.GenerateRefactoringMenu(); }
//		}

//		public override void OnKeyDown(KeyEventArgs keyEventArgs)
//		{
//			_textBox.Value.TextBox.OnKeyDownEngine(keyEventArgs);
//			base.OnKeyDown(keyEventArgs);
//		}
//
//		public override void OnTextInput(TextCompositionEventArgs textCompositionEventArgs)
//		{
//			_textBox.Value.TextBox.OnTextInputEngine(textCompositionEventArgs);
//			base.OnTextInput(textCompositionEventArgs);
//		}
//		
//		public override ICommand UndoCommand
//		{
//			get { return _textBox.Value.TextBox.UndoCommand; }
//		}
//
//		public override ICommand CopyCommand
//		{
//			get { return _textBox.Value.TextBox.CopyCommand; }
//		}
//
//		public override ICommand RedoCommand
//		{
//			get { return _textBox.Value.TextBox.RedoCommand; }
//		}
//
//		public override ICommand PasteCommand
//		{
//			get { return _textBox.Value.TextBox.PasteCommand; }
//		}
//
//		public override ICommand DeleteCommand
//		{
//			get { return _textBox.Value.TextBox.DeleteCommand; }
//		}
//
//		public override ICommand SelectAllCommand
//		{
//			get { return _textBox.Value.TextBox.SelectAllCommand; }
//		}
//
//		public override ICommand CutCommand
//		{
//			get { return _textBox.Value.TextBox.CutCommand; }
//		}

		public override IList<StatusItemBase> StatusElements
		{
			get
			{
				StatusItemBase[] statusItems = new StatusItemBase[] { _statusReady, _statusLinesCount };
				return statusItems;
			}
		}
	}
}
